#include "CIrrDevicePVR.h"
#include <Keycodes.h>
#include <IEventReceiver.h>
#include "EventSender_3dijoy.h"

#ifdef _IRR_COMPILE_WITH_3DIJOY_CTL_

#pragma comment (lib, "./Input3dijoy/lib/3DiJoyApi.lib")

irr::CIrrDevicePVR* getDeviceFromHWnd(HWND hWnd); //<
irr::CIrrDevicePVR* dev=0;

const int KEY_LIM_S = BUTTON_RIGHT - BUTTON_UP + 1;
const int KEY_OFFSET_BASE = BUTTON_UP;
static irr::EKEY_CODE axis2key[KEY_LIM_S]={irr::KEY_UP,irr::KEY_DOWN,irr::KEY_LEFT,irr::KEY_RIGHT};

bool EventSender_3dijoy::Init(void* hWnd)
{
  this->hWnd = hWnd;

  DeviceInit();
  BOOL ready = BluetoothDongleReady();
  if(ready == FALSE)
    return false;

  MessageBox(0, "For Connect 3dijoy control", "PTR", MB_OK); //<

  for(int i=0; i<4; ++i)
  {
    JoyDevice jDev;
    if( SearchDevice(&jDev.deviceId,&jDev.deviceInfo) != 0 )
    { // failed!
      continue;
    }
    else
    {
      devices.push_back(jDev);
      if(devices.size())
        break;
    }
  }


  if(devices.empty())
    return false;

#define BIT_ACC 0x01
#define BIT_DESKTOP_MOUSE 0x02
#define BIT_AXIS 0x04

  for(int i=0; i<devices.size(); ++i)
  {
    UINT8& id = devices[i].deviceId;
    if( CreateConnection(id) == 0 )
    { // success
      SetOutputMode(id, BIT_ACC, TRUE);
      SetOutputMode(id, BIT_DESKTOP_MOUSE, TRUE);
      SetOutputMode(id, BIT_AXIS, TRUE);
    }
  }


  return true;
}

void EventSender_3dijoy::Term()
{
  for(int i=0; i<devices.size(); ++i)
  {
    UINT8& id = devices[i].deviceId;
    ReleaseConnection(id);
  }
}

void EventSender_3dijoy::_OccurAnAxisEvent(UINT16 keyCode, bool pressed)
{
  irr::SEvent event;
  event.EventType = irr::EET_KEY_INPUT_EVENT;
  event.KeyInput.Key = axis2key[keyCode - KEY_OFFSET_BASE];
  event.KeyInput.PressedDown = pressed;
  WORD KeyAsc=0;
  GetKeyboardState(allKeys);
  ToAscii(event.KeyInput.Key,0,allKeys,&KeyAsc,0);
  event.KeyInput.Char = (KeyAsc & 0x00ff); //KeyAsc >= 0 ? KeyAsc : 0;

  dev = getDeviceFromHWnd((HWND)hWnd); //<
  if (dev)
    dev->postEventFromUser(event);
}

void EventSender_3dijoy::PostEvent2Irr()
{
  for(int i=0; i<devices.size(); ++i)
  {
    UINT8& id = devices[i].deviceId;
    BOOL hasRead=FALSE;
    do
    {
      hasRead = ReadRemoteDeviceData(id, &data);
    }while(!hasRead);
  }

  irr::SEvent event;
  event.EventType = irr::EET_MOUSE_INPUT_EVENT;
  event.MouseInput.Event = irr::EMIE_MOUSE_MOVED;
  event.MouseInput.X = data.Mpos.RelPosX;
  event.MouseInput.Y = data.Mpos.RelPosY;
  dev = getDeviceFromHWnd((HWND)hWnd); //<

  if (dev)
  {
    printf("(%d, %d)\n", event.MouseInput.X, event.MouseInput.Y);
    dev->postEventFromUser(event);
  }

  if(data.Buttons[BUTTON_LEFT] == TRUE)
    _OccurAnAxisEvent(BUTTON_LEFT, true);
  else
    _OccurAnAxisEvent(BUTTON_LEFT, false);

  if(data.Buttons[BUTTON_UP] == TRUE)
    _OccurAnAxisEvent(BUTTON_UP, true);
  else
    _OccurAnAxisEvent(BUTTON_UP, false);

  if(data.Buttons[BUTTON_RIGHT] == TRUE)
    _OccurAnAxisEvent(BUTTON_RIGHT, true);
  else
    _OccurAnAxisEvent(BUTTON_RIGHT, false);

  if(data.Buttons[BUTTON_DOWN] == TRUE)
    _OccurAnAxisEvent(BUTTON_DOWN, true);
  else
    _OccurAnAxisEvent(BUTTON_DOWN, false);
}

#endif
